﻿var visitViewVM = {
    VisitID: ko.observable(),
    PatientID: ko.observable(),
    PatientName: ko.observable(),
    CreateDoctorID: ko.observable(),
    CreateDoctorName: ko.observable(),
    CreateDate: ko.observable(),
    UpdateDoctorID: ko.observable(),
    UpdateDoctorName: ko.observable(),
    UpdateDate: ko.observable(),
    Symptom: ko.observable(),
    Diagnosis: ko.observable(),
    Treatment: ko.observable(),
    initialData: function () {
        var self = this;
        self.VisitID('...');
        self.PatientID('...');
        self.PatientName('...');
        self.CreateDoctorID('...');
        self.CreateDoctorName('...');
        self.CreateDate('...');
        self.UpdateDoctorID('...');
        self.UpdateDoctorName('...');
        self.UpdateDate('...');
        self.Symptom('...');
        self.Diagnosis('...');
        self.Treatment('...');
    },
    loadRequest: null,
    cancelLoadRequest: function () {
        var self = this;
        if (self.loadRequest != null) {
            self.loadRequest.abort();
        }
    },
    loadVisit: function (visitID, indexController) {
        var self = this;
        //clear old data;
        self.initialData();
        indexController.message.processing("loading");
        //Ajax call
        self.loadRequest = $.ajax({
            type: 'post',
            url: home + 'Patient/GetVisit',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ id: visitID }),
            success: function (data, textStatus) {
                self.VisitID(data[0].VisitID);
                self.CreateDoctorID(data[0].CreateDoctorID);
                self.CreateDoctorName(data[0].CreateDoctorName);
                self.CreateDate(data[0].CreateDate);
                self.UpdateDoctorID(data[0].UpdateDoctorID);
                self.UpdateDoctorName(data[0].UpdateDoctorName);
                self.UpdateDate(data[0].UpdateDate);
                self.Symptom(data[0].Symptom);
                self.Diagnosis(data[0].Diagnosis);
                self.Treatment(data[0].Treatment);
                indexController.message.processComplete("load_complete");
            },
            error: function (err) {
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                }
                else {
                    indexController.message.processComplete("load_error");
                }
            }
        });
    }
};